package com.leyou.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.dto.SpecParamDTO;
import com.leyou.mapper.SpecGroupMapper;
import com.leyou.mapper.SpecParamMapper;
import com.leyou.pojo.SpecGroup;
import com.leyou.pojo.SpecParam;
import com.leyou.service.SpecGroupService;
import com.leyou.service.SpecParamService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jshstart
 * @creat 2020-12-09 17:21
 */
@Service
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam> implements SpecParamService {
    /**
     * 根据分类和规格组id查询所有的规格参数
     * @param groupId
     * @param categoryId
     * @return
     */
    @Override
    public List<SpecParam> queryByCategoryIdByGroupId(Long groupId, Long categoryId) {
        return this.baseMapper.queryByCategoryIdByGroupId(groupId,categoryId);
    }

    /**
     * 根据条件查询规格参数的集合
     * @param categoryId
     * @param groupId
     * @param searching
     * @return
     */
    @Override
    public List<SpecParamDTO> querySpecParamDTOByCondition(Long categoryId, Long groupId, Boolean searching) {
        if (categoryId==null&&groupId==null){
            throw new RuntimeException("关键值不能为空！");
        }
        List<SpecParam> specParamList = query()
                .eq(!StringUtils.isEmpty(categoryId), "category_id", categoryId)
                .eq(!StringUtils.isEmpty(groupId), "group_id", groupId)
                .eq(searching!=null, "searching", searching).list();
        return specParamList.stream().map(SpecParamDTO::new).collect(Collectors.toList());
    }

    /**
     * 根据分类id查询对应的所有规格参数
     * @param cid3
     * @param searching
     * @return
     */
    @Override
    public List<SpecParamDTO> queryByCategoryAndSearch(Long cid3, Boolean searching) {
        List<SpecParam> specParamList = query().eq("category_id", cid3).eq(searching != null, "searching", searching).list();
        List<SpecParamDTO> dtoList = specParamList.stream().map(SpecParamDTO::new).collect(Collectors.toList());
        return dtoList;
    }
}
